import { getAllChannel, getUserChannel } from '@/api/channel'
export default {
  namespaced: true,
  state: {
    channelList: [],
    allChannelList: []
  },
  mutations: {
    setChannelList (state, payload) {
      state.channelList = payload
    },
    setAllChannel (state, payload) {
      state.allChannelList = payload
    },
    addChannel (state, payload) {
      state.channelList.push(payload)
    },
    delChannel (state, payload) {
      state.channelList = state.channelList.filter(item => +item.id !== +payload.id)
    }
  },
  actions: {
    async getChannelList (context) {
      const res = await getUserChannel()
      context.commit('setChannelList', res.data.channels)
    },
    async getAllChannelList (context) {
      const res = await getAllChannel()
      context.commit('setAllChannel', res.data.channels)
    }
  },
  getters: {
    optionalChannel: state => {
      return state.allChannelList.filter(channel => {
        return state.channelList.findIndex(item => +item.id === +channel.id) === -1
      })
    }
  }
}
